﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Tetris_Online___Client.ScreenManager
{
    class MenuScreen: Screen
    {
        Song theme;
        Vector2 cursorPos;
        MouseState mouseState;
        KeyboardState oldKeyState;
        KeyboardState keyState;

        FormElements.TextBox serverAdress;

        public override void Initialize()
        {
            
        }

        public override void UnloadContent(ContentManager content)
        {
            MediaPlayer.Stop();
            content.Unload();
            content.Dispose();
            base.UnloadContent(content);
        }

        public override void LoadContent(ContentManager content)
        {
            cursorPos = new Vector2();
            theme = content.Load<Song>("songs\\theme");
            MediaPlayer.Play(theme);
            MediaPlayer.IsRepeating = true;

            serverAdress = new FormElements.TextBox(new Vector2(280, 300), 25, Font.small);
            
            base.LoadContent(content);
        }

        public override void Update()
        {
            mouseState = Mouse.GetState();
            cursorPos.X = mouseState.X;
            cursorPos.Y = mouseState.Y;

            oldKeyState = keyState;
            keyState = Keyboard.GetState();

            serverAdress.Update(keyState, oldKeyState);

            base.Update();
        }

        public override void Render2D()
        {
            Basic.gDevice.Clear(Color.Black);
            drawCenterString(Font.big, "TETRIS", 100, Color.Gold);
            drawCenterString(Font.medium, "Online", 200, Color.White);
            Basic.spriteBatch.DrawString(Font.small, "Server:", new Vector2(120, 300), Color.LightBlue);

            serverAdress.Render2D(Basic.spriteBatch, Font.small);

            Basic.spriteBatch.DrawString(Font.small, "Copyright 2013 by LeonAdi", new Vector2(600, 650), Color.DarkGray);
            Basic.spriteBatch.Draw(Texture.cursor, cursorPos, Color.White);
            base.Render2D();
        }

        private void drawCenterString(SpriteFont font, String text, float y, Color color)
        {
            float length = font.MeasureString(text).X;
            float x = Basic.Width / 2 - length / 2;
            Basic.spriteBatch.DrawString(font, text, new Vector2(x, y), color);
        }
    }
}
